package com.kobe.java8;

import com.kobe.lang.LinkHashMapDemo;
import org.springframework.core.convert.converter.Converter;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class MapsDemo {
    public MapsDemo() {
    }

    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();

        for (int i = 0; i < 10; i++) {
            map.putIfAbsent(i, "val" + i);
        }

        map.forEach((id, val) -> System.out.println(val));//val0 val1 val2 val3 val4 val5 val6 val7 val8 val9

        Supplier<MapTest> mapTest = ()->new MapTest(); //MapTest::new;
        mapTest.get().froEach((k,v)->{
            System.out.println(k+v);
        });

        Function<String,MapTest> mapTest2 = (params)->new MapTest(params);
        mapTest2.apply("999").froEach((k,v)->{
            System.out.println(k+v);
        });

        Predicate<String> handler = (s)->s == null || s.length() == 0;
        handler.test("123");
        handler.negate().test("123");



        List<String> a = List.of("1","2","3");
        a.parallelStream();
        a.stream();

        Function<String, Integer> toInteger = Integer::valueOf;
        Function<String, String> backToString = toInteger.andThen(String::valueOf);
        String apply = backToString.apply("123");// "123"

        int num = 1;
        Converter<Integer, String> stringConverter =
                (from) -> String.valueOf(from + num);

        stringConverter.convert(2);     // 3


        WeakHashMap<String,String> stringWeakHashMap = new WeakHashMap<>();
        stringWeakHashMap.put("!","123");
        stringWeakHashMap.put("A","456");
        stringWeakHashMap.put("Q","789");

        System.out.println(stringWeakHashMap.get("!"));
        System.out.println(stringWeakHashMap.get("A"));
        System.out.println(stringWeakHashMap.get("Q"));

        TreeMap<String,String> treeMap = new TreeMap<>();
        for (int i = 0; i < 20; i++) {
            treeMap.put(i+"TreeMap",i+"");
        }
        treeMap.forEach((k,v)->{
            System.out.println(k+"----"+v);
        });
        LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>();

        Vector vector = new Vector();

        System.out.println(1<<3);

        Map<String,String> hashTable = new Hashtable<>();

        System.out.println(123123223 & 0x7FFFFFFF);


        ConcurrentHashMap<String,String> concurrentHashMap = new ConcurrentHashMap<String,String>();


    }

    public static class  MapTest{


        private String k= "123";
        private String v = "456";

        public MapTest() {
        }

        public MapTest(String k) {
            this.k = k;
        }

        public boolean isEmpty(){
            return k == null;
        }

        public void froEach(BiConsumer<String, String> consumer){
             consumer.accept(k,v);
        }
    }
}
